package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {
    @Reference
    private MenuService menuService;

    @GetMapping("page")
    public Result queryMenuPage(QueryPageBean queryPageBean) {
        PageResult pageResult = menuService.querymenuPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        if (pageResult != null) {
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, pageResult);
        }
        return new Result(false, MessageConstant.QUERY_MENU_FAIL);
    }


    /**
     * 根据菜单id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
//    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findById(@PathVariable("id") Integer id) {
        try {
            //查询
            Menu menu = menuService.findById(id);
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

    /**
     * 新增主菜单
     *
     * @param menu
     * @param permissionIds
     * @return
     */
    @PostMapping
    public Result addMenu(
            @RequestBody Menu menu,
            Integer[] permissionIds) {
        try {
            menuService.addMenu(menu, permissionIds);
            return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.ADD_MENU_FAIL);
        }
    }

    /**
     * 编辑菜单
     * @param menu
     * @param permissionIds
     * @return
     */
    @PutMapping
    public Result updateMenu(@RequestBody Menu menu
                                , Integer[] permissionIds) {

        try {
            menuService.updateMenu(menu, permissionIds);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_MENU_FAIL);
        }

    }


    /**
     * 删除菜单
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteMenu(@PathVariable Integer id){
        try {
            menuService.deleteMenu(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping
    @PreAuthorize("hasAuthority('MENU_QUERY')")
    public Result findAll() {
        try {
            List<Menu> menuList = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MENU_FAIL);
        }
    }

}
